import os 
import sys
import time
import math
import json
import csv
from operator import itemgetter
from flask.json import jsonify, dumps, loads

#---------------------LOADING FILES----------------------------

cdict = {}
with open("countryref.csv","rb") as csvfile:
     reader = csv.reader(csvfile, delimiter=',', quotechar='"')
     for row in reader:
     	cdict[row[11]] = row[10]
#print cdict

clmapfile = "clmap.json"
print "CLMAPFILE: " + clmapfile

qc = sys.argv[2]
print "QCLASS: " + qc

adatafile = sys.argv[1] + ".json"
print "ADATAFILE: " + adatafile

tsvfile = "out_qc"+qc+"_"+sys.argv[1]+".tsv"
print "TSVFILE: " + tsvfile

adata = json.loads(open(adatafile).read())
#print "============ADATA=================\n" + json.dumps(adata, sort_keys=True, indent=4, separators=(',', ': ')) + "\n\n\n\n\n\n"

clmap = json.loads(open(clmapfile).read())
#print "============CLMAP=================\n" + json.dumps(clmap, sort_keys=True, indent=4, separators=(',', ': ')) + "\n\n\n\n\n\n"

#--------------------------------------------------------------

def getlangs(c3c):
	if c3c not in cdict:
		return {}
	c2c = cdict[c3c]
	if c2c in clmap:
		return clmap[c2c]["langs"]
	else:
		return {}

lla = {}

#row = 0
for adatum in adata:
	if adatum["qclass_code"] != qc:
		continue
	#row += 1
	#break if row == 10:
	lcf = getlangs(adatum["from_code"])
	lct = getlangs(adatum["to_code"])
	fta = adatum["count_actions"]
	for lcfi in lcf:
		for lcti in lct:
			pct = (lcf[lcfi]*lct[lcti])*fta/10000
			if lcfi in lla:
				if lcti in lla[lcfi]:
					lla[lcfi][lcti] += pct
				else:
					lla[lcfi][lcti] = pct
			else:
				lla[lcfi] = {lcti:pct}

#print json.dumps(lla, sort_keys=True, indent=4, separators=(',', ': '))

#with open("out_qc"+qc+"_"+adatafile, 'w') as outfile:
#	json.dump(lla, outfile, sort_keys = True, indent = 4, separators=(',', ': '), ensure_ascii=False)

tsvdata = [["from_lang","to_lang","count_actions"]]
for lcfi in sorted(lla):
	for lcti in sorted(lla[lcfi]):
		tsvdata.append([lcfi,lcti,lla[lcfi][lcti]])

with open(tsvfile, 'wb') as outfile:
    writer = csv.writer(outfile, delimiter='\t')
    for row in tsvdata:
        writer.writerow(row)

#with open(csvfile, 'rb') as infile:
#	print infile.read()

